package com.atguigu.school.commons;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

@ApiModel(value = "分页信息")
public class Page<T> implements Serializable {

    private static final long serialVersionUID = 8545996863226528798L;

    @ApiModelProperty(value = "当前页，从 1 开始")
    private Integer current;

    @ApiModelProperty(value = "每页长度，从 1 开始")
    private Integer size;

    @ApiModelProperty(value = "查询出的记录列表")
    private List<T> records;

    @ApiModelProperty(value = "总条数")
    private Integer total;

    @ApiModelProperty(value = "总页数")
    private Integer pages;

    @ApiModelProperty(value = "当前页第一条数据的偏移量，数据库查询时使用，从 0 开始")
    private Integer offset;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Page<?> page = (Page<?>) o;
        return Objects.equals(current, page.current) && Objects.equals(size, page.size) && Objects.equals(records, page.records) && Objects.equals(total, page.total) && Objects.equals(pages, page.pages) && Objects.equals(offset, page.offset);
    }

    @Override
    public int hashCode() {
        return Objects.hash(current, size, records, total, pages, offset);
    }

    @Override
    public String toString() {
        return "Page{" +
                "current=" + current +
                ", size=" + size +
                ", records=" + records +
                ", total=" + total +
                ", pages=" + pages +
                ", offset=" + offset +
                '}';
    }

    public Page() {
    }

    public Page(Integer current, Integer size, Integer total) {
        this.current = current;
        this.size = size;
        this.total = total;
        this.offset = (current - 1) * size;
        this.setPages();
    }

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
    }

    public Integer getCurrent() {
        return current;
    }

    public Integer getSize() {
        return size;
    }

    /**
     * 设置当前页、每页长度，并自动设置偏移量、总页数
     * 页码从 1 开始
     * 偏移量从 0 开始
     *
     * @param current 当前页
     * @param size    每页长度
     */
    public void setCurrentAndSize(Integer current, Integer size) {
        this.current = current;
        this.size = size;
        this.offset = (current - 1) * size;
        this.setPages();
    }

    public Integer getPages() {
        return pages;
    }

    /**
     * 根据总条数和每页长度自动计算页数
     */
    private void setPages() {
        this.pages = this.total / this.size;

        if (this.total % this.size > 0) {
            pages++;
        }

    }

    public Integer getOffset() {
        return offset;
    }

}
